package com.example.mallsystem1104.controller;

import com.example.mallsystem1104.common.Result;
import com.example.mallsystem1104.constants.Constants;
import com.example.mallsystem1104.entity.Address;
import com.example.mallsystem1104.exception.ServiceException;
import com.example.mallsystem1104.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zy
 * @Date 2024/12/12 17:13
 * @packageName:com.example.mallsystem1104.controller
 * @ClassName:AddressController
 */
@RestController
@RequestMapping("/api/address")
@CrossOrigin
public class AddressController {
    @Resource
    private AddressService addressService;

    @GetMapping("/{userId}")
    public Result findAllById(@PathVariable Long userId){
        return Result.success(addressService.findAllById(userId));
    }

    @GetMapping
    public Result findAll(){
        List<Address> list=addressService.list();
        return Result.success(list);
    }

    @PostMapping
    public Result save(@RequestBody Address address){
        boolean save = addressService.save(address);
        if(save){
            return Result.success();
        }else {
            throw new ServiceException(Constants.CODE_500,"保存地址失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody Address address){
        return Result.success(addressService.updateById(address));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        addressService.removeById(id);
        return Result.success();
    }
}
